var ipc = require('electron').ipcRenderer

var closeEl = document.querySelector('.close');
closeEl.addEventListener('click',function(){
	ipc.send('close-main-window');
})

var goBtn = document.querySelector('#go-btn');
var inputBar = document.querySelector('#input-bar');
//webView对象
var webView = document.querySelector('#surf');
var webViewStatus = document.querySelector('#webview-status');

//浏览器前后跳转按钮 <-   ->
var webViewHistory = [{"num":0,"address":"about:blank"}];
var number = 0;
var previous = 0;
var present = 0;
var next = 0;
var goGack = document.querySelector('.go-back');
var goForward = document.querySelector('.go-forward');

//地址栏跳转按钮
goBtn.addEventListener('click',function(){
	webView.loadURL(inputBar.value);
	/*
	var inputContent = inputBar.value;
	webView.setAttribute("src",inputContent);
	//先判断是否是同一地址，如果跳转的地址相同不会重复计入浏览历史
	if (inputContent != webViewHistory[present].address) {
		//跳转地址不同,调整flags
		previous = present;
		present++;
		next = present;
		if (present < webViewHistory.length) {
			//浏览记录,删除多余的记录
			for (var i = 0;i <= webViewHistory.length-present; i++) {
				console.log(webViewHistory.pop());
				console.log("pop once!");
			};
			console.log(webViewHistory);
		};//end of inner if
			//add浏览记录
			var webpage = {"num":++number,"address":inputContent};
			var length = webViewHistory.push(webpage);
			console.log("push once!");
			console.log(webViewHistory);
	};//end of outer if
	*/

});

//goGack浏览器回退

goGack.addEventListener('click',function(){
	webView.goBack();
	inputBar.value = webView.getAttribute("src");
	/*
	//判断flags是否到头，取前一个页面地址，并跳转
	if (present>previous){
		//跳转
		var backUrl = webViewHistory[previous].address;
		webView.setAttribute("src",backUrl);
		if (previous > 0) {
			previous--;
		}//end of inner if
		if (present < next){
			next--;
		}
		present--;
	}else{
		console.log("已经到达最初基地，无法继续撤退。");
	}//end of outer if
	*/
})//end of goGack

//goForward浏览器前进
goForward.addEventListener('click',function(){
	webView.goForward();
	inputBar.value = webView.getAttribute("src");
	/*
	//判断flags是否到头，取一个页面地址，并跳转
	if (present<next){
		//跳转
		var forwardUrl = webViewHistory[next].address;
		webView.setAttribute("src",forwardUrl);
		if (next < webViewHistory.length-1){
			next++;
		} //end of inner if
		present++;
		if (present < next){
			previous++;
		}
		
	}else{
		console.log("已经到达此战场最前线，前方没有战场可以继续前进。");
	}//end of outer if
	*/
})//end of goForward

//网页状态栏显示加载成功 —— “ page loaded success.”
webView.addEventListener('load-commit',function(){
	webViewStatus.innerHTML = " page loaded success."
	setTimeout(function(){
		webViewStatus.innerHTML = "";
	},1850);
})

webView.addEventListener('did-finish-load',function(){
	if('file:///html/page-failed-to-load.html' != webView.getAttribute("src")){
		inputBar.value = webView.getAttribute("src");
	}
	
})
//网页状态栏显示开始加载页面 —— “ page loading.....”
webView.addEventListener('did-start-loading',function(){
	webViewStatus.innerHTML = " page loading....."
})
//
webView.addEventListener('did-fail-load',function(){
	webViewStatus.innerHTML = " this page failed to load..."

	webview.src = 'file:///html/page-failed-to-load.html';
})
//Event: 'did-get-redirect-request'
webView.addEventListener('did-get-redirect-request',function(){
	webViewStatus.innerHTML = " a redirect was received..."
})

//const webview = document.querySelector('webview')
webView.addEventListener('dom-ready', () => {
  //webview.openDevTools()
})

webView.addEventListener('new-window',(e)=>{
	const protocol = require('url').parse(e.url).protocol
	if (protocol === 'http:' || protocol === 'https:'){
		console.log('go to url:'+e.url);
	}
})

webView.addEventListener('will-navigate',(e)=>{
	inputBar.value = e.url;
})
//Fired when the guest page attempts to close itself.
webView.addEventListener('close',(e)=>{
	webview.src = 'about:blank';
})

onload = () => {
	console.log("onload commit!")
}